Skip to content

Server: stop per-node subscriptions to DefaultPermissionsChanged - #5211

Merged
marcschier merged 2 commits into
masterfrom
romanett/n14-monitored-node-perm-stamp
Nov 30, 2026
Merged

marcschier merged 2 commits into
masterfrom
romanett/n14-monitored-node-perm-stamp

Conversation

@romanett

@romanett romanett commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Part of the NodeManager lifetime follow-ups (#4931), item N14.

Problem

Every MonitoredNode2 subscribed to the server-wide IConfigurationNodeManager.DefaultPermissionsChanged event when its first item was added, and unsubscribed when its last item was removed. Every SamplingGroup did the same on startup and shutdown. Each += / -= rebuilds the multicast invocation list, an array as long as all the subscribers. Adding or removing a monitored item therefore cost O(monitored nodes on the server), and reloading a manager with many monitored nodes was quadratic. The #5168 baseline (f4-sampled-churn: 20 managers × 500 variables, one reload per second) attributes half of the reload allocation to that object[] in MonitoredNode2.Remove.

Change

  • New internal DefaultPermissionsVersion: one subscription per configuration node manager, which counts its notifications. It is held in a ConditionalWeakTable keyed on the manager and subscribes exactly once. Each counter starts in its own 2^32 range, so a verdict stamped against a replaced configuration manager never matches.
  • MonitoredNode2 folds the counter into the generation that stamps its cached data-change verdicts, event verdicts and event-restriction verdicts (PermissionGeneration). Add, Remove and Dispose no longer touch the event.
  • SamplingGroup.PermissionEpoch folds in the same counter instead of a subscription per group. A group also no longer misses a change that arrives between Shutdown and the next Startup.

Semantics are unchanged: every notification still invalidates every cached verdict before it is next used. The counter is captured before validation and compared again before the verdict is stored, the same way m_permissionGeneration already worked. A change that never raises the event is handled as before: the registry and namespace-default cache stamps (NamespaceDefaultPermissionReader.Generation, static-defaults checks) are untouched.

I chose this over a per-namespace counter in the #5183 cache. The event fires for exactly the changes it covers today, including defaults behind read callbacks, namespaces without cache entries and custom IConfigurationNodeManager implementations. Narrowing invalidation to a single namespace would be a separate optimisation, and today's event invalidates every namespace too.

Measurements

Micro-measure (new test AddAndRemoveItemAllocationIsIndependentOfMonitoredNodeCount, net10.0): bytes allocated per add+remove of one item on one node.

10 monitored nodes 5,010 monitored nodes
master 3,681 B 134,434 B
this PR 1,074 B 1,074 B

The test fails on master: 130,753 B more at 5,010 nodes.

f4-sampled-churn A/B (lifecycle-reload --load 0 --readers 0 --lc-managers 20 --lc-vars 500 --lc-sampling 100 --lc-interval 1000 --lc-remove-every 0 --duration 15 --warmup 8; the control line is the same command with --lc-interval 0). Setup: master 567363fad against this branch, Release net10.0, Server GC, bins at C:\un14m and C:\un14b, 4 rounds with the first side alternating, foreign load 0 cores in every run, 16 reloads per run with 0 failures on both sides. Each cell is the median [min..max].

master this PR
Server alloc / 15 s, churn 872.6 MB [870.0..875.5] 433.2 MB [431.6..435.7]
Server alloc / 15 s, control 55.2 MB [52.8..57.1] 54.2 MB [53.7..56.3]
Alloc per reload, net 51.1 MB 23.7 MB (−54 %)
Server CPU / 15 s, churn 2,773 ms [2,188..2,984] 1,742 ms [1,547..2,094]
Server CPU / 15 s, control 570 ms [453..625] 547 ms [391..688]
CPU per reload, net ~138 ms ~75 ms (−46 %)
Apply p50 / p99 88 / 152 ms 31 / 120 ms

The net figures subtract the control median and divide by 16 reloads. The #5168 baseline recorded about 54 MB per reload on master.

Tests

  • MonitoredNode2Tests: the dispose test now asserts that a node never subscribes. A new test checks that three nodes share one subscription, that cached verdicts are reused, and that one notification re-validates every node. InFlightDataPermissionCannotReviveInvalidatedGrantAsync(defaultsChanged: true) still covers the in-flight race.
  • Opc.Ua.Server.Tests, net10.0, full run: 12,539 passed, 9 failed. Eight failures are the symlink tests, which need a privilege this machine lacks. PreparedBatchImmediateCutoffPreservesCapturedNativeReadAsync(True) failed under full load and passed 3 times out of 3 when run alone.
  • net48, subset (MonitoredNode / SamplingGroup / Permission / MonitoredItem / Subscription / NamespaceMetadata / ConfigurationNodeManager / Authorization): 2,602 passed, 0 failed.

🤖 Generated with Claude Code

…ter (N14)

Every MonitoredNode2 subscribed to the server-wide
IConfigurationNodeManager.DefaultPermissionsChanged event on its first item
and unsubscribed on its last, and every SamplingGroup did the same on
startup. Each += / -= rebuilds the multicast invocation list, an array as
long as all subscribers, so adding or removing a monitored item cost
O(monitored nodes on the server) and a reload of a manager with many
monitored nodes was quadratic.

DefaultPermissionsVersion now subscribes once per configuration node manager
and counts the notifications. Monitored nodes fold the counter into the
generation that stamps their cached data-change, event and event-restriction
verdicts; sampling groups fold it into their permission epoch. A
notification still invalidates every verdict before its next use, as
before, but adding and removing items no longer touches the event.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Code coverage

✅ Coverage gate passed.

Check Result Threshold
✅ Project line rate 89.50% (515271/575722 lines) >= 70.00%
✅ Project branch rate 80.15% >= 60.00%
✅ Patch coverage 100.00% (34/34 changed lines) >= 60.00% (<= 100 changed lines, advisory)
ℹ️ Baseline delta (advisory) +15.90 pp 73.60% recorded

Coverage is above the recorded baseline - consider ratcheting coverage-thresholds.json.

Thresholds live in coverage-thresholds.json. Whole report before exclusions: line 89.05%, branch 79.66%.

@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.85%. Comparing base (567363f) to head (692b239).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5211      +/-   ##
==========================================
- Coverage   83.87%   83.85%   -0.02%     
==========================================
  Files        3878     3879       +1     
  Lines      575722   575722              
  Branches    99274    99270       -4     
==========================================
- Hits       482893   482779     -114     
- Misses      60129    60227      +98     
- Partials    32700    32716      +16     
Flag Coverage Δ
actions 83.85% <100.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...Manager/MonitoredItem/DefaultPermissionsVersion.cs 100.00% <100.00%> (ø)
....Server/NodeManager/MonitoredItem/MonitoredNode.cs 91.41% <100.00%> (-0.15%) ⬇️
....Server/NodeManager/MonitoredItem/SamplingGroup.cs 92.32% <100.00%> (-1.63%) ⬇️

... and 59 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@romanett romanett changed the title Server: stop per-node subscriptions to DefaultPermissionsChanged (N14) Server: stop per-node subscriptions to DefaultPermissionsChanged Nov 30, 2026
@romanett
romanett marked this pull request as ready for review November 30, 2026 04:31
Copilot AI balanced review requested due to automatic review settings November 30, 2026 04:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A race publishes the subscription flag before the handler is attached, allowing a defaults notification to be lost and a stale permissions verdict to be cached.

1 open issue

Review effort: Balanced

A concurrent first caller could see the subscribed flag before the handler
was attached and stamp a verdict with a counter that could still miss a
notification. Subscribe under a lock and publish the flag after the handler
is attached; a regression test gates the event add accessor with two
concurrent callers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The reported full-load concurrency test failure, despite isolated passes, requires human confirmation of final CI results and its disposition because the change affects shared authorization-cache invalidation.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@romanett

Copy link
Copy Markdown
Contributor Author

On the local failure of PreparedBatchImmediateCutoffPreservesCapturedNativeReadAsync(True) mentioned in the description: it threw BadSubscriptionIdInvalid after 20 s. On this machine that is the known signature of the shared test session expiring (10 s timeout) while the host is stalled. That run was a 23-minute full suite with another load benchmark on the same machine. It is not a permission verdict: the change can only make a cached verdict re-validate more often, never drop a subscription. The test passed 3 out of 3 times on its own, and the whole Server.Tests suite is green on both net10.0 and net48 in CI on 692b239.

🤖 Addressed by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants